package com.swt.draw.controller;

import com.swt.draw.dao.PostMapper;
import com.swt.draw.entity.Admin;
import com.swt.draw.entity.Post;
import com.swt.draw.example.PostExample;
import com.swt.draw.vo.BaseResponse;
import com.swt.draw.vo.LayTableResponseVO;
import javafx.geometry.Pos;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/9/1 0001 16:01
 * @Version 1.0
 **/
@RestController
@RequestMapping("/postManager")
public class PostController {

    @Resource
    private PostMapper postMapper;


    /**
     * @Author ZPF
     * @Description 添加岗位
     * @Date 16:37 2019/9/1 0001
     * @Param []
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/addPost")
    public BaseResponse addPost(@RequestBody Post post){
        BaseResponse response = new BaseResponse();
        if(post == null){
            return  response;
        }
        if(post.getPostStatus() == null){
            post.setPostStatus((short) 1);
        }
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        String postNo = new StringBuffer("role_").append(UUID.randomUUID()).toString();
        post.setPostNo(postNo);
        post.setPostCreateBy(admin.getAdminNo());
        post.setPostCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        postMapper.insertSelective(post);
        response.setCode(1001);
        response.setDesc("success");
        return response;
    }

    /**
     * @Author ZPF
     * @Description 获取岗位列表
     * @Date 16:02 2019/9/1 0001
     * @Param []
     * @return com.swt.draw.vo.LayTableResponseVO<com.swt.draw.entity.Post>
     **/
    @RequestMapping("/getPostList")
    public LayTableResponseVO<Post> getPostList(Integer page, Integer limit){
        LayTableResponseVO responseVO = new LayTableResponseVO();
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        criteria.andPostStatusNotEqualTo((short) 2);
        Long count = postMapper.countByExample(example);
        Integer start = (page - 1) * limit;
        Integer end = page * limit;
        List<Post> posts = postMapper.getPostList(start, end);
        responseVO.setCount(count);
        responseVO.setData(posts);
        responseVO.setCode(1001);
        responseVO.setMsg("success");
        return responseVO;
    }

    /**
     * @Author ZPF
     * @Description 删除岗位
     * @Date 16:20 2019/9/1 0001
     * @Param []
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/delPost")
    public BaseResponse delPost(@RequestParam(value = "postNos[]") String[] postNos){
        BaseResponse response = new BaseResponse();
        if(postNos == null || postNos.length == 0){
            return response;
        }
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        criteria.andPostNoIn(Arrays.asList(postNos));
        Post post = new Post();
        post.setPostStatus((short) 2);
        postMapper.updateByExampleSelective(post, example);
        response.setCode(1001);
        response.setDesc("success");
        return response;
    }

    /**
     * @Author ZPF
     * @Description 编辑岗位
     * @Date 16:32 2019/9/1 0001
     * @Param []
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/editPost")
    public BaseResponse editPost(@RequestBody Post post){
        BaseResponse response = new BaseResponse();
        if(post == null){
            return response;
        }
        if(post.getPostStatus() == null){
            post.setPostStatus((short) 1);
        }
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        criteria.andPostNoEqualTo(post.getPostNo());
        postMapper.updateByExampleSelective(post, example);
        response.setCode(1001);
        response.setDesc("success");
        return response;
    }
}
